<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>面试API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 5px;
            white-space: pre-wrap;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
    </style>
</head>
<body>
    <h1>面试API测试页面</h1>
    
    <div class="test-section">
        <h3>1. 开始面试测试</h3>
        <button onclick="testStartInterview()">开始面试</button>
        <div id="startResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 获取题目测试</h3>
        <input type="text" id="interviewId" placeholder="面试ID" value="1">
        <input type="text" id="questionOrder" placeholder="题目序号" value="1">
        <button onclick="testGetQuestion()">获取题目</button>
        <div id="questionResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>3. 提交答案测试</h3>
        <input type="text" id="answerInterviewId" placeholder="面试ID" value="1">
        <input type="text" id="questionId" placeholder="题目ID" value="1">
        <textarea id="answerText" placeholder="答案内容" rows="3" cols="50">这是一个测试答案</textarea>
        <button onclick="testSubmitAnswer()">提交答案</button>
        <div id="answerResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 获取面试历史</h3>
        <button onclick="testGetHistory()">获取历史</button>
        <div id="historyResult" class="result"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:8081/api';
        
        // 模拟登录token（实际使用时需要先登录获取）
        const mockToken = 'mock-token-for-testing';
        
        async function makeRequest(url, options = {}) {
            try {
                const response = await fetch(url, {
                    ...options,
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${mockToken}`,
                        ...options.headers
                    }
                });
                
                const data = await response.json();
                return { success: true, data, status: response.status };
            } catch (error) {
                return { success: false, error: error.message };
            }
        }
        
        async function testStartInterview() {
            const resultDiv = document.getElementById('startResult');
            resultDiv.textContent = '正在测试...';
            
            const testData = {
                interviewerType: 1,
                targetPosition: "Java后端开发工程师",
                questionTypes: ["Java基础", "MySQL"],
                questionCount: 3
            };
            
            const result = await makeRequest(`${API_BASE_URL}/interview/start`, {
                method: 'POST',
                body: JSON.stringify(testData)
            });
            
            if (result.success) {
                resultDiv.className = 'result success';
                resultDiv.textContent = `成功: ${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.className = 'result error';
                resultDiv.textContent = `失败: ${result.error}`;
            }
        }
        
        async function testGetQuestion() {
            const resultDiv = document.getElementById('questionResult');
            const interviewId = document.getElementById('interviewId').value;
            const questionOrder = document.getElementById('questionOrder').value;
            
            resultDiv.textContent = '正在测试...';
            
            const result = await makeRequest(`${API_BASE_URL}/interview/question?interviewId=${interviewId}&questionOrder=${questionOrder}`);
            
            if (result.success) {
                resultDiv.className = 'result success';
                resultDiv.textContent = `成功: ${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.className = 'result error';
                resultDiv.textContent = `失败: ${result.error}`;
            }
        }
        
        async function testSubmitAnswer() {
            const resultDiv = document.getElementById('answerResult');
            const interviewId = document.getElementById('answerInterviewId').value;
            const questionId = document.getElementById('questionId').value;
            const answerText = document.getElementById('answerText').value;
            
            resultDiv.textContent = '正在测试...';
            
            const testData = {
                interviewId: parseInt(interviewId),
                questionId: parseInt(questionId),
                textAnswer: answerText
            };
            
            const result = await makeRequest(`${API_BASE_URL}/interview/answer/text`, {
                method: 'POST',
                body: JSON.stringify(testData)
            });
            
            if (result.success) {
                resultDiv.className = 'result success';
                resultDiv.textContent = `成功: ${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.className = 'result error';
                resultDiv.textContent = `失败: ${result.error}`;
            }
        }
        
        async function testGetHistory() {
            const resultDiv = document.getElementById('historyResult');
            resultDiv.textContent = '正在测试...';
            
            const result = await makeRequest(`${API_BASE_URL}/interview/history`);
            
            if (result.success) {
                resultDiv.className = 'result success';
                resultDiv.textContent = `成功: ${JSON.stringify(result.data, null, 2)}`;
            } else {
                resultDiv.className = 'result error';
                resultDiv.textContent = `失败: ${result.error}`;
            }
        }
    </script>
</body>
</html>
